/* 
 * Cashbox is the legal property of its developers, whose names are listed in the copyright file included
 * with this source distribution.
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import "CBMenuController.h"

#import "CBImporter.h"
#import "CBDataController.h"

#import "CBInterfaceController.h"
#import "CBMainWindowController.h"

static void *CBMainWindowVisibleTableColumnsChangeContext = @"CBMainWindowVisibleTableColumnsChangeContext";
static void *CBMainWindowAvailableTableColumnsChangeContext = @"CBMainWindowAvailableTableColumnsChangeContext";

@interface CBMenuController (Private)
- (void)rebuildColumnsMenu;
- (void)updateColumnsMenuStates;
@end

@implementation CBMenuController

- (void)controllerDidLoad {
	[self rebuildColumnsMenu];
	
	NSObject <CBInterfaceController> *interface = [cashbox interfaceController];
	[[interface mainWindowController] addObserver:self
									   forKeyPath:@"visibleTableColumns"
										  options:0
										  context:CBMainWindowVisibleTableColumnsChangeContext];
	[[interface mainWindowController] addObserver:self
									   forKeyPath:@"availableTableColumns"
										  options:0
										  context:CBMainWindowAvailableTableColumnsChangeContext];
}

- (void)controllerWillClose {
	
}

- (void)observeValueForKeyPath:(NSString *)keyPath
					  ofObject:(id)object
						change:(NSDictionary *)change
					   context:(void *)context {
	if (context == CBMainWindowAvailableTableColumnsChangeContext) {
		[self rebuildColumnsMenu];
	} else if (context == CBMainWindowVisibleTableColumnsChangeContext) {
		[self updateColumnsMenuStates];
	}
}

#pragma mark interface builder actions
// ----------------------------------------------------------------------------------------------------
// interface builder actions
// ----------------------------------------------------------------------------------------------------

- (IBAction)noAction:(id)sender {
	
}

- (IBAction)import:(id)sender {
	NSOpenPanel *openPanel = [NSOpenPanel openPanel];
	[openPanel setTitle:FSLocalizedString(@"Import", nil)];
	[openPanel setPrompt:FSLocalizedString(@"Choose", nil)];
	
	int returnCode = [openPanel runModalForDirectory:nil file:nil types:[CBImporter handledExtensions]];
	if (returnCode == NSOKButton) {
		[[cashbox dataController] importFile:[openPanel filename]];
	}
}

- (IBAction)newTransaction:(id)sender {
	[[[cashbox interfaceController] mainWindowController] addTransaction:nil];
}
- (IBAction)newTransfer:(id)sender {
	[[[cashbox interfaceController] mainWindowController] addTransferTransaction:nil];
}
- (IBAction)removeTransaction:(id)sender {
	[[[cashbox interfaceController] mainWindowController] removeTransaction:nil];
}
- (IBAction)newReport:(id)sender {
	[[[cashbox interfaceController] mainWindowController] addReport:nil];
}
- (IBAction)removeReport:(id)sender {
	[[[cashbox interfaceController] mainWindowController] removeReport:nil];
}

- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
	if (menuItem == columnsMenuItem) {
		// columns menu is only enabled if there are columns
		return [[[[cashbox interfaceController] mainWindowController] availableTableColumns] count] > 0;
	} else if (menuItem == newTransactionMenuItem) {
		return [[[cashbox interfaceController] mainWindowController] canAddTransaction];
	} else if (menuItem == newTransferMenuItem) {
		return [[[cashbox interfaceController] mainWindowController] canAddTransferTransaction];
	} else if (menuItem == removeTransactionMenuItem) {
		return [[[cashbox interfaceController] mainWindowController] canRemoveTransaction];
	} else if (menuItem == newReportMenuItem) {
		return [[[cashbox interfaceController] mainWindowController] canAddReport];
	} else if (menuItem == removeReportMenuItem) {
		return [[[cashbox interfaceController] mainWindowController] canRemoveReport];
	} else {
		return [self respondsToSelector:[menuItem action]];
	}
}

- (IBAction)donate:(id)sender {
	[[NSWorkspace sharedWorkspace]
	 openURL:[NSURL URLWithString:@"http://www.fadingred.org/cashbox/donate/"]];
}

- (IBAction)contribute:(id)sender {
	[[NSWorkspace sharedWorkspace]
	 openURL:[NSURL URLWithString:@"http://www.fadingred.org/cashbox/contribute/"]];
}

- (IBAction)supportWiki:(id)sender {
	[[NSWorkspace sharedWorkspace]
	 openURL:[NSURL URLWithString:@"http://www.fadingred.org/cashbox/support/"]];
}


#pragma mark columns menu
// ----------------------------------------------------------------------------------------------------
// columns menu
// ----------------------------------------------------------------------------------------------------

- (void)updateColumnsMenuStates {
	NSArray *visible = [[[cashbox interfaceController] mainWindowController] visibleTableColumns];
	NSMenuItem *menuItem;
	NSEnumerator *menuItemEnumerator = [[columnsMenu itemArray] objectEnumerator];
	while (menuItem = [menuItemEnumerator nextObject]) {
		NSTableColumn *column = [menuItem representedObject];
		if ([visible indexOfObjectIdenticalTo:column] != NSNotFound) {
			[menuItem setState:1];
		} else {
			[menuItem setState:0];
		}
	}
}

- (void)rebuildColumnsMenu {
	while ([columnsMenu numberOfItems]) { [columnsMenu removeItemAtIndex:0]; }

	NSArray *available = [[[cashbox interfaceController] mainWindowController] availableTableColumns];
	NSArray *visible = [[[cashbox interfaceController] mainWindowController] visibleTableColumns];
	NSTableColumn *column;
	NSEnumerator *columnEnumerator = [available objectEnumerator];
	while (column = [columnEnumerator nextObject]) {
		NSMenuItem *item = [columnsMenu addItemWithTitle:[[column headerCell] title]
												  action:@selector(toggleTableColumn:)
										   keyEquivalent:@""];
		[item setTarget:self];
		[item setRepresentedObject:column];
		if ([visible indexOfObjectIdenticalTo:column] != NSNotFound) {
			[item setState:1];
		}
	}
}

- (void)toggleTableColumn:(id)sender {
	if ([sender isKindOfClass:[NSMenuItem class]]) {
		[[[cashbox interfaceController] mainWindowController]
		 toggleTableColumnVisibility:[sender representedObject]];
	}
}

@end
